//
//  LXEntertainmentTableViewController.m
//  cat
//
//  Created by imera on 16/1/16.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXEntertainmentTableViewController.h"
#import "SlideBarCell.h"
#import "LXMovieGroupTitleView.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"

@interface LXEntertainmentTableViewController ()

@property (nonatomic, strong) NSArray *videoArr;

@end

@implementation LXEntertainmentTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //获取数据
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    NSString *url = @"http://cat666.com/cat666-interface/index.php/index/getCat/6";
    
    [mgr GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id response) {
        //字典转模型
        NSArray *videos = response;
        
        //缓存数据
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *filePath = [path stringByAppendingPathComponent:@"music.plist"];
        
        [videos writeToFile:filePath atomically:YES];
        
        NSMutableArray *videoArr = [NSMutableArray array];
        for (NSDictionary *dict in videos) {
            LXVideo *video = [[LXVideo alloc]initWithDict:dict];
            [videoArr addObject:video];
        }
        
        if (videoArr.count % 2 == 1) { // 奇数个
            [videoArr removeObjectAtIndex:videoArr.count - 1];
        }
        self.videoArr = videoArr;
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        [MBProgressHUD showError:@"您的网络开小差了..."];
        
        //缓存数据
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *filePath = [path stringByAppendingPathComponent:@"music.plist"];
        
        NSArray *videos = [NSArray arrayWithContentsOfFile:filePath];
        
        NSMutableArray *videoArr = [NSMutableArray array];
        for (NSDictionary *dict in videos) {
            LXVideo *video = [[LXVideo alloc]initWithDict:dict];
            [videoArr addObject:video];
        }
        
        if (videoArr.count % 2 == 1) { // 奇数个
            [videoArr removeObjectAtIndex:videoArr.count - 1];
        }
        self.videoArr = videoArr;
        
        [self.tableView reloadData];
    }];
}

#pragma mark tableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.videoArr.count / 2;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 170;
}

- (SlideBarCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"Cell";
    
    SlideBarCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[SlideBarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.videos = @[self.videoArr[indexPath.row * 2], self.videoArr[indexPath.row * 2 + 1]];
    return cell;
}

@end
